﻿using System.Threading.Tasks;

namespace Sidvall.IO.Compression
{
    public class ZipFile : IZipFile
    {
        #region Public Members

        #region AddEntryAsync

        public Task<IZipEntry> AddEntryAsync(string entryName, System.IO.Stream stream)
        {
            var item = this.InternalZipFile.AddEntry(entryName, stream);
            IZipEntry zipEntry = new ZipEntry(item);
            return Task.FromResult(zipEntry);
        }

        #endregion
        #region SaveAsync

        public Task SaveAsync(string fileName)
        {
            this.InternalZipFile.Save(fileName);
            return Task.FromResult(true);
        }
        public Task SaveAsync(System.IO.Stream outputStream)
        {
            this.InternalZipFile.Save(outputStream);
            return Task.FromResult(true);
        }

        #endregion

        #region Dispose

        public void Dispose()
        {
            if (this.InternalZipFile != null)
            {
                this.InternalZipFile.Dispose();
                this.InternalZipFile = null;
            }
            System.GC.SuppressFinalize(this);
        }

        #endregion

        #endregion
        #region Private Members

        private Ionic.Zip.ZipFile InternalZipFile { get; set; }

        #endregion
        #region Constructors

        public ZipFile()
        {
            this.InternalZipFile = new Ionic.Zip.ZipFile();
        }

        #endregion
    }
}
